package com.tim.third;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;

import java.io.File;
import java.io.FileReader;
import java.util.List;

/**
 * pom文档解析
 * maven-model
 */
public class MavenPomUtils {
  // 包：org.apache.maven#maven-model；https://blog.csdn.net/weixin_31822733/article/details/114389233
  public static MavenXpp3Reader POM_READER = new MavenXpp3Reader();

  public static void main(String[] args) throws Exception {
    String path =
        "E:\\gitlab\\xqxc\\plat\\plat-transaction\\plat-transaction-impl\\src\\test\\java\\com\\xqxc\\api\\impl\\plat\\transaction\\order\\OrderServiceImplTest.java" +
                "";
    // 输出Autil的pom引用
//    StringBuffer buffer = new StringBuffer();
//    get(path, buffer);
//    System.out.println(buffer);

    // 输出plat maven install命令
    printPlatInstallCmd(path);
  }

  public static void printPlatInstallCmd(String path) throws Exception {
    // 1，读取、解析pom文件；拼接依赖
    path = path.trim().replaceAll("\\\\+", "/");
    path = path.split("/src/")[0];
    path = path.endsWith("/") ? path : path+"/";
    String pomPath = path + "pom.xml";
    Model pomModel = POM_READER.read(new FileReader(pomPath));

    // 2，plat开头的依赖；-f指定路径。
    // 打开terminal，执行命令
    pomModel.getDependencies().stream()
            .map(Dependency::getArtifactId)
            .filter(e -> e.startsWith("plat-")||e.startsWith("xqxc-"))
            .map(e->e.endsWith("-api")?e.replace("-api", "/")+e:e)
            .map(e->e.endsWith("-impl")?e.replace("-impl", "/")+e:e)
            .forEach(e -> System.out.println(String.format("mvn clean install -DskipTests -f plat/%s/pom.xml", e)));
  }

  public static void getProjectModule(String path, StringBuffer buffer) throws Exception{
    path = path.trim();
    path = path.endsWith("\\")||path.endsWith("/") ? path : path+"/";
    String pomPath = path + "pom.xml";
    File pomFile = new File(pomPath);
    if(!pomFile.exists()){
      return;
    }
    // 1，读取、解析pom文件；拼接依赖
    Model pomModel = POM_READER.read(new FileReader(pomPath));
    String group = StringUtils.isBlank(pomModel.getGroupId()) ? pomModel.getParent().getGroupId() : pomModel.getGroupId();
    String version = StringUtils.isBlank(pomModel.getVersion()) ? pomModel.getParent().getVersion() : pomModel.getVersion();

//    System.out.println(pomModel.getArtifactId());// 打印项目名称
    buffer.append(String.format(DEPENDENCY_TEMPLATE, group, pomModel.getArtifactId(), version));
    // 2，判定是否有module，有则跳到1
    List<String> modules = pomModel.getModules();
    if(CollectionUtils.isNotEmpty(modules)){
      for(String module : modules){
        String modulePath = path+module;
        getProjectModule(modulePath, buffer);
      }
    }
  }

  public static void getDirModule(String path, StringBuffer buffer) throws Exception{
    path = path.trim();
    path = path.endsWith("\\")||path.endsWith("/") ? path : path+"/";

    File dir = new File(path);
    File[] subDirs = dir.listFiles(File::isDirectory);
    for(File subDir : subDirs){
      StringBuffer subBuffer = new StringBuffer();
      getProjectModule(subDir.getPath(), subBuffer);
      buffer.append(subBuffer);
    }
  }

  public static final String DEPENDENCY_TEMPLATE ="\n"
          + "\t\t<dependency>\n"
          + "\t\t\t<groupId>%s</groupId>\n"
          + "\t\t\t<artifactId>%s</artifactId>\n"
          + "\t\t\t<version>%s</version>\n"
          + "\t\t</dependency>"
          +"\n";
}
